package com.decade.qiyeshapan.controller;

import cn.hutool.json.JSONObject;
import com.decade.qiyeshapan.model.ApiResponse;
import com.decade.qiyeshapan.service.GameService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

import com.decade.qiyeshapan.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import com.alibaba.fastjson.JSON;
import org.springframework.http.ResponseEntity;
import com.decade.qiyeshapan.config.RedisKeyManager;

@RestController
@RequestMapping("/api/game")
public class GameController {

    private final UserService userService;
    private final RedisTemplate<String, Object> redisTemplate;
    private final GameService gameService;

    public GameController(UserService userService, RedisTemplate<String, Object> redisTemplate, GameService gameService) {
        this.userService = userService;
        this.redisTemplate = redisTemplate;
        this.gameService = gameService;
    }

    @PostMapping("/updateProgress")
    public ApiResponse<String> updateProgress(@RequestBody Map<String, String> request) {
        String gameId = request.get("gameId");
        String username = request.get("username");
        String progress = request.get("progress");
        gameService.updatePlayerProgress(gameId, username, Integer.parseInt(progress));
        return ApiResponse.success();
    }

    @GetMapping("/{gameId}/state")
    public ApiResponse<Map<String, Object>> getGameState(
        @PathVariable String gameId, 
        @RequestParam String username
    ) {
        String key = RedisKeyManager.userState(gameId, username);
        String timelineKey = RedisKeyManager.timeline(gameId);
        Object state = redisTemplate.opsForValue().get(key);
        
        // 获取时间线
        String timeline = (String) redisTemplate.opsForValue().get(timelineKey);

        Map<String, Object> result = new HashMap<>();
        result.put("personalState", state != null ? state : new HashMap<>());
        result.put("timeline", timeline);
        
        return ApiResponse.success(result);
    }

    @PostMapping("/save-ad")
    public ApiResponse saveAdvertisement(@RequestBody Map<String, Object> payload) {
        userService.saveAdvertisement(
            payload.get("gameId").toString(),
            payload.get("username").toString(),
            payload.get("region").toString(),
            payload.get("product").toString(),
            Integer.parseInt(payload.get("amount").toString())
        );
        return ApiResponse.success();
    }

    @PostMapping("/save-ads")
    public ApiResponse saveAdvertisements(@RequestBody Map<String, Object> payload) {
        List<Map<String, Object>> ads = (List<Map<String, Object>>) payload.get("ads");
        String gameId = payload.get("gameId").toString();
        String username = payload.get("username").toString();
        
        userService.batchSaveAdvertisements(gameId, username, ads);
        return ApiResponse.success();
    }

    @GetMapping("/{gameId}/available-orders")
    public ApiResponse getAvailableOrders(@PathVariable String gameId) {
        return gameService.getAvailableOrders(gameId);
    }

    @PostMapping("/{gameId}/select-order")
    public ApiResponse selectOrder(
        @PathVariable String gameId,
        @RequestBody Map<String, String> request) {
        
        String orderId = request.get("orderId");
        String username = request.get("username");
        return gameService.selectOrder(gameId, orderId, username);
    }

} 